package com.itheima.controller;

import com.itheima.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @author by itheima
 * @Date 2022/4/25
 * @Description 定义restfull风格的控制器类
 */
//@Controller
//@ResponseBody
@RestController//等价于@Controller+@Responsebody
@RequestMapping("/rest")
public class RestFullController {

//    @RequestMapping(method = RequestMethod.GET)
    @GetMapping
//    @ResponseBody
    public String demo01(){
        System.out.println("findAll");
        return "findAll";
    }

    /**
     * http://localhost:8080/rest/100
     * 路径中的请求参数使用{userId}包括，作为路径中的可变参数
     * 如果获取路径中的参数呢？
     *  使用@PathVariable
     *      特点：如果路径中的变量名称与方法的入参名称不一致，则需要在注解中指定路径中的变量名称
     *          如果一致，则省略不写
     *      注意事项：rest风格路径要与请求路径格式保证一致，否则访问不到
     * @param id
     * @return
     */
//    @RequestMapping(value = "/rest/{userId}",method = RequestMethod.GET)
//    @RequestMapping(value = "/{userId}",method = RequestMethod.GET)
    @GetMapping("/{userId}")
//    @ResponseBody
    public String demo11(@PathVariable("userId") Integer id){
        System.out.println("findById : " + id);
        return "findById";
    }

//    @RequestMapping(value = "/{page}/{pageSize}",method = RequestMethod.GET)
    @GetMapping("/{page}/{pageSize}")
//    @ResponseBody
    public String demo12(@PathVariable Integer page,@PathVariable Integer pageSize){
        System.out.println("findByPage : " + page + "," + pageSize);
        return "findByPage";
    }

//    @RequestMapping(method = RequestMethod.DELETE)
    @DeleteMapping
//    @ResponseBody
    public String demo02(){
        System.out.println("deleteAll");
        return "deleteAll";
    }

    /**
     * 功能：根据id删除信息
     * @param id
     * @return
     */
//    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    @DeleteMapping("/{id}")
//    @ResponseBody
    public String demo21(@PathVariable Integer id){
        System.out.println("deleteById : " + id);
        return "deleteById";
    }

//    @RequestMapping(method = RequestMethod.POST)
    @PostMapping
//    @ResponseBody
    public String demo03(@RequestBody User user){
        System.out.println("add: " + user);
        return "add";
    }


//    @RequestMapping(method = RequestMethod.PUT)
    @PutMapping
//    @ResponseBody
    public String demo04(){
        System.out.println("update");
        return "update";
    }
}
